/*
 * Copyright (C) 2010-2101 Alibaba Group Holding Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.otter.manager.biz.remote.impl;

import com.alibaba.otter.manager.biz.config.channel.ChannelService;
import com.alibaba.otter.manager.biz.config.datamatrix.DataMatrixService;
import com.alibaba.otter.manager.biz.config.node.NodeService;
import com.alibaba.otter.manager.biz.remote.ConfigRemoteService;
import com.alibaba.otter.shared.common.model.config.channel.Channel;
import com.alibaba.otter.shared.common.model.config.channel.ChannelStatus;
import com.alibaba.otter.shared.common.model.config.data.DataMatrix;
import com.alibaba.otter.shared.common.model.config.node.Node;
import com.alibaba.otter.shared.common.utils.JsonUtils;
import com.alibaba.otter.shared.communication.core.CommunicationRegistry;
import com.alibaba.otter.shared.communication.model.config.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Config的remote接口处理
 *
 * @author jianghang 2011-10-21 下午02:53:53
 * @version 4.0.0
 */
public class ConfigRemoteServiceImpl implements ConfigRemoteService {

    private static final Logger logger = LoggerFactory.getLogger(ConfigRemoteServiceImpl.class);

    private ChannelService channelService;
    private NodeService nodeService;
    private DataMatrixService dataMatrixService;

    public ConfigRemoteServiceImpl() {
        // 注册一下事件处理
        CommunicationRegistry.register(ConfigEventType.findChannel, this);
        CommunicationRegistry.register(ConfigEventType.findNode, this);
        CommunicationRegistry.register(ConfigEventType.findTask, this);
        CommunicationRegistry.register(ConfigEventType.findMedia, this);
    }



    /**
     * 根据对应的工作节点机器id，获取相关的channel任务
     */
    public Channel onFindChannel(FindChannelEvent event) {
        Assert.notNull(event,"event not be null");
        Long channelId = event.getChannelId();
        Long pipelineId = event.getPipelineId();
        Channel channel;
        if (channelId != null) {
            channel = channelService.findById(channelId);
        } else {
            Assert.notNull(pipelineId,"pipeline not null ");
            channel = channelService.findByPipelineId(pipelineId);
        }

        return channel;
    }

    public Node onFindNode(FindNodeEvent event) {
        Assert.notNull(event,"event not be null");
        Assert.notNull(event.getNid(),"nid not be null");
        return nodeService.findById(event.getNid());
    }

    public List<Channel> onFindTask(FindTaskEvent event) {
        Assert.notNull(event,"event not be null");
        Assert.notNull(event.getNid(),"nid not be null");
        // 同时查询start/pause状态的同步任务，因为在发布时重启jvm刚好在执行stopNode的restart指令，此时channel处于pause状态，丢失了任务命令
        return channelService.listByNodeId(event.getNid(), ChannelStatus.START, ChannelStatus.PAUSE);
    }

    public String onFindMedia(FindMediaEvent event) {
        Assert.notNull(event,"event not be null");
        Assert.notNull(event.getDataId(),"dataid not be null");
        DataMatrix matrix = dataMatrixService.findByGroupKey(event.getDataId());
        return JsonUtils.marshalToString(matrix);
    }

    // =============== setter / getter ===================


    public void setChannelService(ChannelService channelService) {
        this.channelService = channelService;
    }

    public void setNodeService(NodeService nodeService) {
        this.nodeService = nodeService;
    }

    public void setDataMatrixService(DataMatrixService dataMatrixService) {
        this.dataMatrixService = dataMatrixService;
    }

}
